package com.example.myspring.service;

import com.example.myspring.entity.Student;
import com.example.myspring.mapper.StuDao;
//import com.example.myspring.util.GetSqlSession;
//import com.example.myspring.util.MybatisUtils;
import org.apache.ibatis.annotations.Param;
//import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


//业务层
@Service
public class StudentService {


    private final StuDao studao;


    private final RedisService rs;

    @Autowired
    public StudentService(StuDao studao, RedisService rs) {
        this.studao = studao;
        this.rs = rs;
    }

//    public StudentService(){}
//
//    public void setStudao(StuDao studao){
//        this.studao = studao;
//    }
//



    //在所有增删改操作数据库之后都查询一下数据库用于更新redis缓存
    public List<Student> findAll() {
        List<Student> list = studao.findAll();
        System.out.println(list);
        for (Student student : list) {
            rs.setHash("user:" + student.getName(), student.toMap());
        }
        return list;

    }



    public void addStudent(Student student) {
        studao.addStudentNotPwd(student);
        findAll();
    }

    public int countForNameOrPwd(String name,String pwd) {
//        return studao.countForName(name);
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("password", pwd);


        return studao.countByNameOrPwd(name,pwd);
    }

    public int updateStudent(Student student) {
        return studao.updateStudent(student);
    }



//    public int countForPwd(String pwd){
//        return studao.countForPwd(pwd);
//    }
//
//    public int countForLogin(String name,String pwd){
//        return studao.countForLogin(name,pwd);
//    }
//
//    public Student oneStudent(@Param("name") String name, @Param("pwd")  String pwd){
//        return studao.oneStudent(name,pwd);
//    }

}
